<template>
  <div class="top-category" v-if="localItem">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="fadeBread" mode="out-in">
          <XtxBreadItem :key="localItem.id">{{ localItem.name }}</XtxBreadItem>
        </transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height:500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in localItem.children" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in list" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.desc }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  ref,
  computed,
  defineAsyncComponent,
  watch,
  onBeforeUnmount,
  onUnmounted
} from 'vue'
import { useStore } from 'vuex'
import { useRoute, onBeforeRouteLeave } from 'vue-router'
// import GoodsItem from './components/goods-item.vue'
import { category } from '@/api/category'
export default {
  name: 'TopCategory',
  components: {
    // 异步组件
    GoodsItem: defineAsyncComponent(() => import('./components/goods-item.vue'))
  },
  setup () {
    const sliders = ref([])
    // 面包屑对应字符获取
    // id  list:[{id,name}]
    const store = useStore()
    const route = useRoute()
    const localItem = computed(() => {
      // find:找到了就返回当前项item,找不到返回undefined
      return store.state.category.list.find(item => {
        return item.id === route.params.id
      })
    })
    // 轮播图数据处理
    store.dispatch('home/getBannerList').then(res => {
      sliders.value = res
    })
    // 一级分类数据
    const list = ref([])
    const stop = watch(
      () => route.params.id,
      () => {
        category(route.params.id).then(res => {
          list.value = res.result.children
        })
      },
      {
        immediate: true
      }
    )
    onBeforeRouteLeave(() => {
      stop()
    })

    /*
    A==B
    1：先修改路由
    2：路由地址修改后，当前路由所在组件还在A这
    3：watch这时候侦听到了改变，它就会执行
    生命周期怎么变化的
    vue2.x
    beforeCreate(B)=>created(B)=>beforeMount(B)=>beforeDestroy(A)=>destroyed(A)=>mounted(B)
    vue3.x
    onBeforeUnmount(A)=>setup(B)=>onBeforeMount(B)=>onUnmounted(A)=>onMounted(B)
    路由勾
    vue2.x
    beforeRouteLeave:只能在路由配制所对应组件中使用
    beforeRouteLeave(to,from,next){}
    vue3.x
    onBeforeRouteLeave：所有组件中使用
    onBeforeRouteLeave(to,from){}
    */

    onBeforeUnmount(() => {
      console.log('onBeforeUnmount:一级类目')
    })
    onUnmounted(() => {
      console.log('onUnmounted:一级类目')
    })

    return { sliders, localItem, list }
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
